package com.anuo.app.modules.coach.service;


import com.anuo.app.common.globalhandle.AppException;
import com.anuo.app.common.persistence.PageData;
import com.anuo.app.common.utils.StringUtil;
import com.anuo.app.modules.coach.dao.StudentDao;
import com.anuo.app.modules.coach.entity.*;
import com.anuo.app.modules.user.entity.UserAccount;
import com.anuo.app.modules.venue.entity.Venue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;


@Service
@Transactional(readOnly = true)
public class StudentService {
    @Autowired
    StudentDao studentDao;

    @Transactional(readOnly = false)
    public void saveCoachComment(CoachComment coachComment) {
        coachComment.preInsert();
        studentDao.insertCoachComment(coachComment);
        //System.out.println(count++);
    }

    @Transactional(readOnly = true)
    public PageData getCoachCommentList(CoachFilter coachFilter) {
        PageData pageData = new PageData<CoachComment>();
        if (coachFilter.getPageNumber() == 1) {//第一页获取总行数其他时候不获取(牺牲实时性,提升性能)
            int totalRow=studentDao.getCoachCommentListCount(coachFilter);
            if (totalRow == 0) {
                return pageData;
            }
            pageData.setTotalRow(totalRow);
        }

        List<CoachComment> coachCommentList = studentDao.getCoachCommentList(coachFilter);
        pageData.setEntities(coachCommentList);

        return pageData;
    }


    @Transactional(readOnly = true)
    public int getStudentMoney() {
        return studentDao.getStudentMoney();
    }

    @Transactional(readOnly = true)
    public List<CoachStudentmoney> getStudentBalanceRecords(CoachStudentmoney coachStudentmoney) {
        return studentDao.getStudentBalanceRecords(coachStudentmoney);
    }

    @Transactional(readOnly = true)
    public CoachCourse getCourseAndIsJoined(UserAccount userAccount) {
        return studentDao.getCourseAndIsJoined(userAccount);
    }

    @Transactional(readOnly = false)
    public void deleteCoachCommentBatch(List<CoachComment> coachCommentList) {
        studentDao.deleteCoachCommentBatch(coachCommentList);
    }

    public List<Venue> getLatestVenueVisit() {
        return studentDao.getLatestVenueVisit();
    }

    public CoachCourse getCoachCourseById(String courseId) {
        CoachCourse coachCourse = new CoachCourse();
        coachCourse.setId(courseId);
        return studentDao.getCoachCourse(coachCourse);
    }

    @Transactional(readOnly = true)
    public boolean isHaveBalance(CoachStudentmoney coachStudentmoney) {
        CoachStudentmoney result = studentDao.getStudentBalance(coachStudentmoney);
        if (result == null) {
            return false;
        }
        if (result.getAmount() > 0) {
            return true;
        }
        return false;
    }

    @Transactional(readOnly = true)
    public boolean isJoinState(String state) {
        if (state.equals(CoachDic.COURSE_START)) {
            return true;
        } else {
            return false;
        }

    }

    @Transactional(readOnly = true)
    public boolean isJoinedCourse(CoachCoursejoin coachCoursejoin) {
        boolean result = studentDao.isJoinedCourse(coachCoursejoin);
        return result;
    }

    /**
     * 计算课程空位数
     *
     * @return
     */
    public int countEmptyPosition(int fillPosition, String courseType) {
        switch (courseType) {
            case CoachDic.BIG_COURSE:
                return CoachDic.BIG_COURSE_MAX_PERSON - fillPosition;
            case CoachDic.PRIVATE_COURSE:
                return 1 - fillPosition;
            default:
                return 0;
        }
    }

    @Transactional(readOnly = true)
    public boolean isHaveEmptyPosition(CoachCoursejoin coachCoursejoin) {
        int fillCount = studentDao.getJoinedStudentCount(coachCoursejoin);
        CoachCourse coachCourse = studentDao.getCoachCourseById(coachCoursejoin.getCourseid());
        String courseType = "";
        if (coachCourse != null) {
            courseType = coachCourse.getCourse_type();
        }
        int emptyPositionCount = countEmptyPosition(fillCount, courseType);

        if (emptyPositionCount > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Transactional(readOnly = false)
    public void BalanceSubOne(CoachCoursejoin coachCoursejoin) {
        CoachStudentmoney result = studentDao.getLegalBalanceRecord(coachCoursejoin);
        if (result != null) {
            //减一次
            studentDao.subOneBalanceById(result.getId());
            //插入报名数据
            BigDecimal totalAmount = new BigDecimal(result.getThen_totalamount());
            BigDecimal thenUnitPrice = result.getThen_totalmoney().divide(totalAmount);
            coachCoursejoin.setThen_course_unitprice(thenUnitPrice);
            coachCoursejoin.preInsert();
            studentDao.insertCoachCourseJoin(coachCoursejoin);
        }
    }


    @Transactional(readOnly = false)
    public void courseNotJoin(List<CoachCoursejoin> coachCoursejoinList) {
        for (CoachCoursejoin coachCoursejoin : coachCoursejoinList) {
            if (StringUtil.isEmpty(coachCoursejoin.getCourseid())
                    || StringUtil.isEmpty(coachCoursejoin.getStudentid())
                    || StringUtil.isEmpty(coachCoursejoin.getCoachid())) {
                continue; //只要有一个为空就不满足条件
            }

            if (!studentDao.isExistJoinedRecord(coachCoursejoin)) {
                //不存在报名记录, 提示如下信息
                throw new AppException("课程已取消, 不能再取消");
            }

            //删除此学员的课程报名 记录
            studentDao.deleteCourseJoin(coachCoursejoin);
            //返还一次余额
            CoachStudentmoney coachStudentmoney = studentDao.getLegalBalanceRecord(coachCoursejoin);
            if (coachStudentmoney != null) {
                studentDao.addOneBalanceById(coachStudentmoney.getId());
            }

        }

    }


}